﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using 开单.Dao.基础;
using 开单.Model.基础;
using 开单.ComMethod;

namespace 开单.Frm.基础
{
    public partial class FrmYhxx : Form
    {
        YhxxDao yhxxDao = new YhxxDao();
        YhxxEntity yhxxEntity = new YhxxEntity();
        DataTable dt = new DataTable();
        private static FrmYhxx Instance;
        private FrmYhxx()
        {
            InitializeComponent();
            uClistMoreYh.ListEvent += new ClientApplication.ComControl.UClistMore.ListEventHandler(uClistMoreYh_ListEvent);
            RefeashDtv();
        }

        public static FrmYhxx GetInstance()
        {
            if (null == Instance || Instance.IsDisposed)
            {
                Instance = new FrmYhxx();
                //Instance.MdiParent = GlobalVar.frmMain;
            }
            Instance.BringToFront();
            return Instance;
        }

        private void RefeashDtv()
        {
            DataTable dtAll = yhxxDao.GetYhxxDt("");
            dataGridView1.DataSource = dtAll;
            dtAll.Dispose();
        }

        void uClistMoreYh_ListEvent(开单.ComMethod.MyListItem myList)
        {
            TxtYh.Text = myList.TextField;
            TxtYh.Tag = myList.ValueField;
            uClistMoreYh.Visible = false;
            TxtYh.Focus();
        }


        private void TxtYh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (0 != dt.Rows.Count)
                {
                    if (dt.Rows[0][1].ToString() == TxtYh.Text) { return; }
                }
                dt = yhxxDao.GetYhxxDt(TxtYh.Text.Trim());
                uClistMoreYh.addData(dt);
                uClistMoreYh.BringToFront();
            }
        }

        private void toolStripQuery_Click(object sender, EventArgs e)
        {
            if (null != dt)
            {
                dt = yhxxDao.GetYhxxDtByYhxm(TxtYh.Text.Trim());
                dataGridView1.DataSource = dt;
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.RowCount == 0) { return; }
            if (e.RowIndex == -1) { return; }
            TxtYh.Tag = dataGridView1["账号编码", e.RowIndex].Value;
            TxtYhName.Text = dataGridView1["账号", e.RowIndex].Value.ToString();
            TxtYhPsw.Text=dataGridView1["密码", e.RowIndex].Value.ToString();
            if (dataGridView1["可用标志", e.RowIndex].Value.ToString() == "是")
            {
                ChkKybz.Checked = true;
            }
            else
            {
                ChkKybz.Checked = false;
            }
            
            TxtYhxm.Text=dataGridView1["真实姓名", e.RowIndex].Value.ToString();
            TxtYhjp.Text = dataGridView1["简拼", e.RowIndex].Value.ToString();

            groupBox3.Visible = true;
            button2.Visible = false;
            button1.Visible = true;
        }

        private void toolStripRefeash_Click(object sender, EventArgs e)
        {
            RefeashDtv();
            groupBox3.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!(MessageBox.Show("确定要修改吗？", "提示", MessageBoxButtons.YesNo,
              MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)) { return; }
            yhxxEntity.yh_code = Convert.ToInt32(TxtYh.Tag.ToString());
            yhxxEntity.yh_name = TxtYhName.Text;
            yhxxEntity.yh_psw = TxtYhPsw.Text;
            if (ChkKybz.Checked == true)
            {
                yhxxEntity.yh_kybz = "是";
            }
            else
            {
                yhxxEntity.yh_kybz = "否";
            }
            yhxxEntity.yh_xm = TxtYhxm.Text;
            yhxxEntity.yh_jp = TxtYhjp.Text;
            yhxxDao.UpdataYhxx(yhxxEntity);
            RefeashDtv();
            MessageBox.Show("修改成功");
            IntityControl();
            groupBox3.Visible = false;
        }

        private void IntityControl()
        {
            TxtYh.Tag = null;
            TxtYhName.Text = "";
            TxtYhPsw.Text = "";
            ChkKybz.Checked = true;
            TxtYhxm.Text = "";
            TxtYhjp.Text = "";
        }

        private void TxtYhxm_TextChanged(object sender, EventArgs e)
        {
            this.TxtYhjp.Text = GetPYString.GetPYStr(TxtYhxm.Text);
        }

        private void toolStripClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripAdd_Click(object sender, EventArgs e)
        {
            groupBox3.Visible = true;
            button2.Visible = true;
            button1.Visible = false;
            TxtYhName.Focus();
            IntityControl();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!(IsOkToAdd())) { return; };
            if (!(MessageBox.Show("确定要增加吗？", "提示", MessageBoxButtons.YesNo,
            MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)) { return; }
            //yhxxEntity.yh_code = Convert.ToInt32(TxtYh.Tag.ToString());
            yhxxEntity.yh_name = TxtYhName.Text;
            yhxxEntity.yh_psw = TxtYhPsw.Text;
            if (ChkKybz.Checked == true)
            {
                yhxxEntity.yh_kybz = "是";
            }
            else
            {
                yhxxEntity.yh_kybz = "否";
            }
            yhxxEntity.yh_xm = TxtYhxm.Text;
            yhxxEntity.yh_jp = TxtYhjp.Text;
            yhxxDao.AddYhxx(yhxxEntity);
            RefeashDtv();
            MessageBox.Show("修改成功");
            IntityControl();
        }

        private bool IsOkToAdd()
        {
            if (TxtYhName.Text.Trim() == "") { MessageBox.Show("请输入账号"); TxtYhName.Focus(); return false; }
            if (TxtYhPsw.Text.Trim() == "") { MessageBox.Show("请输入密码"); TxtYhPsw.Focus(); return false; }
            if (TxtYhxm.Text.Trim() == "") { MessageBox.Show("请输入真实名字"); TxtYhxm.Focus(); return false; }
            
            return true;
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            // 把第4列显示*号，*号的个数和实际数据的长度相同
            if (e.ColumnIndex == 3)
            {
                if (e.Value != null && e.Value.ToString().Length > 0)
                {
                    e.Value = new string('*', e.Value.ToString().Length);
                }
            }
        }


    }
}
